//
//  BaseTableView.m
//  ZiChanBao
//
//  Created by liujinliang on 15/6/11.
//  Copyright (c) 2015年 WorldUnion. All rights reserved.
//

#import "BaseTableView.h"
#import "MJRefresh.h"
#import "UIView+Extension.h"
#import "NSString+Extension.h"
#import "Macro.h"
#import "UIColor+Extension.h"

@implementation BaseTableView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    if (self = [super initWithFrame:frame style:style])
    {
        self.backgroundColor = [UIColor clearColor];
        
        UIView *empty = [[UIView alloc] init];
        empty.backgroundColor = [UIColor clearColor];
        self.tableFooterView = empty;
        self.separatorColor = [UIColor colorWithWhite:0.15f alpha:0.2f];
        [self setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    }
    return self;
}

#pragma mark - 刷新
#pragma mark 顶部刷新
- (void)addHeaderRefreshTarget:(id)target action:(SEL)action
{
//    if (self.header && [self.header isKindOfClass:[MJRefreshNormalHeader class]]) return;
//    
//    self.header = [MJRefreshNormalHeader headerWithRefreshingTarget:target refreshingAction:action];

}

/**
 *  主动进入刷新状态
 */
- (void)beginHeaderRefresh
{
    [self.header beginRefreshing];
}

- (void)endHeaderRefreshing
{
    if (self.header)
    {
        [self.header endRefreshing];
    }
}

- (void)removeHeaderRefresh
{
    [self removeHeader];
}

#pragma mark 底部刷新
- (void)addFooterRefreshTarget:(id)target action:(SEL)action
{
    if (self.footer && [self.footer isKindOfClass:[MJRefreshAutoNormalFooter class]]) return;
    self.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:target refreshingAction:action];
    
    
//    MJRefreshLegendFooter *footer = [self addLegendFooterWithRefreshingTarget:target refreshingAction:action];
//    footer.textColor = [UIColor fontGray];
//    footer.font = [UIFont fontContent];
}

- (void)endFooterRefreshing
{
    if (self.footer)
    {
        [self.footer endRefreshing];
    }
}

- (void)removeFooterRefresh
{
    [self removeFooter];
}

- (void)placeholderViewShow:(BOOL)isShow
{
    if (isShow) {
        [self setTableFooterView:self.placeView];
    }else{
        [self setTableFooterView:nil];
    }
}

- (UIView *)placeView{
    if (!_placeView) {
        _placeView = [[UIView alloc] initWithFrame:self.bounds];
        [_placeView setBackgroundColor:[UIColor clearColor]];
    }
//    if (!_placeControl) {
        [_placeView addSubview:self.placeControl];
//    }
//    if (!_placeLabel) {
        [_placeView addSubview:self.placeLabel];
//    }
//    if (!_placeImageView) {
        [_placeView addSubview:self.placeImageView];
//    }
    return _placeView;
}

- (UIImageView *)placeImageView{
    if (!_placeImageView) {
        _placeImageView = [[UIImageView alloc] init];
        _placeImageView.image = _placeImage==nil? [UIImage imageNamed:@"icon__wuxiaoxi"]:_placeImage;
        [_placeImageView setContentMode:UIViewContentModeCenter];
    }
    CGSize imgSize = _placeImageView.image.size;
    CGFloat viewY = (self.height-_placeLabel.height-imgSize.height)/2;
    [_placeImageView setFrame:(CGRect){(self.width-imgSize.width)/2,viewY,imgSize}];
    if ([NSString notEmptyOrNull:_placeLabel.text]) {
        _placeLabel.y = CGRectGetMaxY(_placeImageView.frame)+kMargin;
    }
    return _placeImageView;
}

- (UILabel *)placeLabel{
    if (!_placeLabel) {
        _placeLabel = [[UILabel alloc] initWithFrame:(CGRect){kMargin,0,self.width-2*kMargin,0}];
        [_placeLabel setBackgroundColor:[UIColor clearColor]];
        [_placeLabel setFont:kFont(36)];
        _placeLabel.text = IsStringEmpty(_placeStr) ? kToast_Message_NoData_ : _placeStr;
        [_placeLabel setFrame:(CGRect){kMargin,0,self.width-2*kMargin,50}];
//        [_placeLabel sizeOfStringWithMaxHeight:60];
        _placeLabel.font = kFont(30.0);
        [_placeLabel setTextColor:[UIColor backgroundColor]];
        [_placeLabel setTextAlignment:NSTextAlignmentCenter];
    }
    return _placeLabel;
}

- (UIActivityIndicatorView *)lodingView{
    if (!_lodingView) {
        _lodingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        [_lodingView setFrame:(CGRect){0,0,20,20}];
    }
    return _lodingView;
}

- (UIControl *)placeControl{
    if (!_placeControl) {
        _placeControl = [[UIControl alloc] initWithFrame:self.bounds];
        [_placeControl setBackgroundColor:[UIColor clearColor]];
    }
    return _placeControl;
}
@end
